<?php

namespace App\Http\Resources\Trip\Orders;

use App\Trip\Enums\TripTypes;
use App\Trip\Orders\TripOrderState;
use App\Trip\Orders\TripOrderStateDesc;
use App\Trip\Orders\TripType;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class OrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $res                     = parent::toArray($request);
        $res['can_delete']       = in_array($res['order_state'], TripOrderState::can_delete_states()) ? 1 : 0;
        $res['is_valid']         = in_array($res['order_state'], TripOrderState::valid_states()) ? 1 : 0;
        $pay_end                 = Carbon::parse($res['created_at'])->addHour();
        $res['cancel_time_diff'] = Carbon::now()->greaterThan($pay_end) ? 0 : $pay_end->diffInSeconds(Carbon::now());
        if ($res['trip_type'] == TripType::TRIP_TYPE_TRAIN) {
            $res['passengers'] = OrderListPassengersResource::collection($res['train_passengers']);
            unset($res['train_passengers']);
            $is_snap = false;
            if (isset($res['train_order'])) {
                $is_snap = $res['train_order']['snap_up'] == TripOrderState::ORDER_SNAP_UP;
            }

            if ($is_snap && TripOrderState::canReplaceSnapState($res['order_state'])) {
                $res['order_state'] = $res['train_order']['order_snap_state'];
            }

            $res['train_info'] = new OrderTrainResource($res['train_info']);
        } elseif ($res['trip_type'] == TripType::TRIP_TYPE_PLANE) {
            $res['passengers'] = OrderListPassengersResource::collection($res['plane_passengers']);
            unset($res['plane_passengers']);
            $res['flight_info'] = new OrderFlightResource($res['flight_info']);
        }
        $trip_type               = TripTypes::from($res['trip_type']);
        $res['order_state_desc'] = TripOrderStateDesc::transDescByOrderState($trip_type, $res['order_state']);

        if ($request->offsetGet('index_query') == 1 && $trip_type->isTrain()) {
            $start            = Carbon::parse($res['train_info']['start_at']);
            $arrive           = Carbon::parse($res['train_info']['arrive_at']);
            $res['trip_info'] = [
                'trip_code'       => $res['train_info']['train_code'] ?? '',
                'train_code'      => $res['train_info']['train_code'] ?? '',
                'from_name'       => $res['train_info']['from_station_name'] ?? '',
                'from_name_uy'    => $res['train_info']['from_station_name_uy'] ?? '',
                'to_name'         => $res['train_info']['to_station_name'] ?? '',
                'to_name_uy'      => $res['train_info']['to_station_name_uy'] ?? '',
                'start_date'      => $start->toDateString(),
                'arrive_date'     => $arrive->toDateString(),
                'start_time'      => $start->format("H:i"),
                'arrive_time'     => $arrive->format("H:i"),
                'start_time_diff' => Carbon::now()->lessThan($start) ?
                    Carbon::now()->diffInMinutes($start) : 0,
                'order_amount'    => $res['order_amount'],
            ];
            unset($res['train_info']);
        }

        unset($res['id'], $res['user_id']);
        return $res;
    }
}
